<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline"  >
      <el-form-item label="岗位编码">
        <el-input v-model="formInline.postCode" placeholder="岗位编码"></el-input>
      </el-form-item>
      <el-form-item label="岗位名称">
        <el-input v-model="formInline.postName" placeholder="岗位名称" ></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="formInline.status" placeholder="状态" clearable>
          <el-option  label="正常" value="0"></el-option>
          <el-option  label="停用" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="list">查询</el-button>
        <el-button type="primary" icon="el-icon-refresh-right" @click="reset">刷新</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            plain
            size="mini"
            type="primary"
            @click="openAdd"
            icon="el-icon-plus">添加
<!--            v-hasPermi="['system:post:add']"-->

        </el-button>
        <el-button
            plain
            icon="el-icon-edit"
            size="mini"
            type="success"
            @click="openUpdate"
            :disabled="single">修改
<!--            v-hasPermi="['system:post:edit']"-->
        </el-button>
        <el-button
            plain
            icon="el-icon-delete"
            size="mini"
            type="warning"
            @click="remove"
            :disabled="multiple"
            v-hasPermi="['system:post:remove']"
        >删除</el-button>
        <el-button
            plain
            icon="el-icon-download"
            size="mini"
            type="danger"
            @click="excel"
            v-hasPermi="['system:post:export']"
        >导出</el-button>
        <el-upload
            class="upload-demo"
            drag
            action="https://jsonplaceholder.typicode.com/posts/"
            multiple>
          <i class="el-icon-upload"></i>

          <div class="el-upload__text"><em>导入excel</em></div>
          <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
        <el-button @click="getxz" type="success">下载模版</el-button>
      </el-col>
    </el-row>

    <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          fixed
          prop="postId"
          label="岗位ID"
          width="150">
      </el-table-column>
      <el-table-column
          prop="postCode"
          label="岗位编码"
          width="120">
      </el-table-column>
      <el-table-column
          prop="postName"
          label="岗位名称"
          width="120">
      </el-table-column>
      <el-table-column
          prop="postSort"
          label="显示顺序"
          width="120">
      </el-table-column>
      <el-table-column
          prop="status"
          label="状态"
          width="120">
        <template v-slot="s">
<!--          {{s.row.status==0?'正常':'停用'}}-->
            <el-tag v-if="s.row.status==0">正常</el-tag>
            <el-tag v-if="s.row.status==1" type="danger">停用</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="createBy"
          label="创建者"
          width="120">
      </el-table-column>
      <el-table-column
          prop="createTime"
          label="创建时间"
          width="200">
      </el-table-column>
      <el-table-column
          prop="pic"
          label="图片"
          width="200">
        <template v-slot="s">
          <el-image
            style="width: 100px;height: 100px"
            :src="s.row.pic"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column
          prop="updateBy"
          label="更新者"
          width="120">
      </el-table-column>
      <el-table-column
          prop="updateTime"
          label="更新时间"
          width="200">
      </el-table-column>
      <el-table-column
          prop="remark"
          label="备注"
          width="120">
      </el-table-column>
      <el-table-column
          fixed="right"
          label="操作"
          width="120">
        <template slot-scope="scope">
          <el-button class="el-icon-edit" @click="openUpdate(scope.row)" type="text" size="small">修改</el-button>
          <el-button class="el-icon-delete" @click="del(scope.row.postId)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        @current-change="goPage"
        :page-size="10"
        layout="total, prev, pager, next"
        :total="total">
    </el-pagination>

    <el-dialog title="添加岗位" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="ruleForm">
        <el-form-item label="岗位名称" :label-width="formLabelWidth" prop="postName">
          <el-input v-model="form.postName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="岗位编码" :label-width="formLabelWidth" prop="postCode">
          <el-input v-model="form.postCode" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="岗位顺序" :label-width="formLabelWidth" prop="postSort">
          <el-input-number v-model="form.postSort" controls-position="right"  :min="1" :max="10"></el-input-number>
        </el-form-item>
        <el-form-item label="岗位状态" :label-width="formLabelWidth">
          <el-radio v-model="form.status" label="0">正常</el-radio>
          <el-radio v-model="form.status" label="1">停用</el-radio>
        </el-form-item>
        <el-form-item label="图片上传" :label-width="formLabelWidth">
          <el-upload
              class="avatar-uploader"
              action="http://localhost:8100/system/post/addPic"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
              v-model="form.remark">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="add('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改岗位" :visible.sync="dialogFormVisible1">
      <el-form :model="form1">
        <el-form-item label="岗位名称" :label-width="formLabelWidth1">
          <el-input v-model="form1.postName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="岗位编码" :label-width="formLabelWidth1">
          <el-input v-model="form1.postCode" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="岗位顺序" :label-width="formLabelWidth1">
          <el-input-number v-model="form1.postSort" controls-position="right"  :min="1" :max="10"></el-input-number>
        </el-form-item>
        <el-form-item label="岗位状态" :label-width="formLabelWidth1">
          <el-radio v-model="form1.status" label="0">正常</el-radio>
          <el-radio v-model="form1.status" label="1">停用</el-radio>
        </el-form-item>
        <el-form-item label="图片上传" :label-width="formLabelWidth">
          <el-upload
              class="avatar-uploader"
              action="http://localhost:8100/system/post/addPic"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth1">
          <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
              v-model="form1.remark">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "PostView",
  data() {
    return {
      formInline:{},
      tableData:[],
      pageNum:1,
      total:0,
      form:{},
      dialogFormVisible: false,
      formLabelWidth: '120px',
      dialogFormVisible1: false,
      formLabelWidth1: '120px',
      form1:{},
      rules: {
        postName: [
          {required: true, message: '请输入岗位名称', trigger: 'blur'},
        ],
        postCode: [
          {required: true, message: '请输入岗位编码', trigger: 'blur'},
        ],
        postSort: [
          {required: true, message: '请输入岗位顺序', trigger: 'blur'},
        ],
      },
      options: [{
        value: '选项1',
        label: '正常'
      }, {
          value: '选项2',
          label: '停用'
      }],
      value: '',
      single:true,
      multipleSelection:[],
      ids:[],
      multiple:true,
      imageUrl:'',

    }
  }, methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.pic=res;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    reset(){
      window.location.reload();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      for (let i = 0; i < val.length; i++) {
        this.ids[i]=val[i].postId;
      }
      console.log(this.ids)
      if(val.length!=1){
        this.single=true;
      }else {
        this.single=false;
      }

      if(val.length!=0){
        this.multiple=false;
      }else {
        this.multiple=true;
      }
    },
    remove(){
      this.axios.post("/system/post/deletes?ids="+this.ids).then(res=>{
        if (res.data.code==200){
          this.$message.success("删除成功");
          this.list();
        }else{
          this.$message.error("删除失败");
        }
      })
    },
    xiugai(){
      //const postId = row.postId;


    },
    goPage(pageNum){
      this.pageNum=pageNum;
      this.list();
    },
    list(){
      this.axios.post("/system/post/list?pageNum="+this.pageNum,this.formInline).then(res=>{
        this.tableData=res.data.data.list;
        this.total=res.data.data.total;
      })
    },
    openAdd(){
      this.dialogFormVisible=true;
      this.form={status:'0'};
    },
    add(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.axios.post("/system/post/add",this.form).then(res=>{
            if (res.data.code==200){
              this.dialogFormVisible=false;
              this.$message.success(res.data.msg);
              this.list();
            }else{
              this.$message.error(res.data.msg);
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    openUpdate(row){
      if(row){
        this.dialogFormVisible1=true;
        this.form1=row;
        this.imageUrl=row.pic;
        this.list();
      }else {
        let id =this.ids[0];
        this.axios.get("/system/post/postById?id="+id).then(res=>{
          this.form1=res.data.data;
          this.dialogFormVisible1=true;
        })
      }
    },
    update(){
      this.axios.post("/system/post/update",this.form1).then(res=>{
        if (res.data.code==200){
          this.dialogFormVisible1=false;
          this.$message.success(res.data.msg);
          this.list();
        }else{
          this.$message.error(res.data.msg);
        }
      })
    },
    del(postId){
      this.axios.post("/system/post/delete?postId="+postId).then(res=>{
        if (res.data.code==200){

          this.$message.success(res.data.msg);
          this.list();
        }else{
          this.$message.error(res.data.msg);
        }
      })
    },
    excel(){
      window.location.href=this.axios.defaults.baseURL+"system/post/export";
    },
    getxz() {
      window.location.href="http://localhost:9000/test/menus/menus.xlsx"
    },
  }, created() {
    this.list();
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>